package com.fan.leetcode;

/**
 * 581. 最短无序连续子数组
 *
 * @author ZhangFan
 * @since 2021/8/3 下午 10:11
 */
public class Question581 {
    public int findUnsortedSubarray(int[] nums) {
        int start = -1, end = -2;
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[i] > nums[j]) {
                    start = i;
                    break;
                }
            }
            if (start != -1) {
                break;
            }
        }
        for (int i = nums.length - 1; i > -1; i--) {
            for (int j = i - 1; j > -1; j--) {
                if (nums[i] < nums[j]) {
                    end = i;
                    break;
                }
            }
            if (end != -2) {
                break;
            }
        }
        return end - start + 1;
    }
}
